package com.sgcc.platforminterface;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.sgcc.pda.cepriaidl.AIDLUtils;
import com.sgcc.pda.data.dao.impl.ContentProviderDaoImpl;
import com.sgcc.pda.data.dao.impl.KnowlsContentProviderDaoImpl;
import com.sgcc.pda.data.dao.impl.MessageProviderDaoImpl;
import com.sgcc.pda.gis.GisManager;
import com.sgcc.pda.hw.impl.CommonUtilManager;
import com.sgcc.pda.hw.impl.CommonUtilManager2;
import com.sgcc.pda.hw.impl.HardwareManager;
import com.sgcc.pda.hw.impl.ImageOCRManager;
import com.sgcc.pda.hw.impl.Protocol376Manager;
import com.sgcc.pda.hw.impl.Protocol645Manager;
import com.sgcc.pda.hw.impl.Protocol698Manager;
import com.sgcc.pda.hw.impl.ProtocolIECManager;
import com.sgcc.pda.hw.impl.ProtocolWSManager;
import com.sgcc.pda.hw.impl.ScannerManager;
import com.sgcc.pda.hw.impl.SealManager;
import com.sgcc.pda.hw.impl.SecurityUnitManager;
import com.sgcc.pda.hw.impl.SecurityUnitManager2;
import com.sgcc.pda.hzaidl.AIDLServiceImpl;
import com.sgcc.pda.safe.AIDLSafeServiceImpl;
import com.sgcc.pda.safe.impl.SafeManager;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.pda.util.PlatConstant;
import com.sgcc.pda.web.WebManager;
import com.sgcc.pda.web.WebManager2;

/**
 * @创建者：suven on 2020/3/25 13:37
 * @描述：
 * @版本：1.0
 * @修改人：刘畅
 * @修改时间：2020/09/17 14:00
 * @修改说明：增加安全管控平台服务极速版绑定、解绑、启用状态设置和获取功能
 * @修改版本：1.1
 */
public class PlatfInterfManager {
    /**
     * 单例
     */
    private static PlatfInterfManager mInstance;
    /**
     * 上下文
     */
    private Context context;
    /**
     * 极速版标志
     */
    private boolean isSpeed = false;

    //初始化
    public static PlatfInterfManager getInstance() {
        if (mInstance == null) {
            mInstance = new PlatfInterfManager();
        }
        return mInstance;
    }

    /**
     * 初始化
     *
     * @param context 上下文
     * @return
     */
    public PlatfInterfManager init(Context context) {
        return this.init(context, isZoomApp(context));
    }

    /**
     * 初始化
     *
     * @param context 上下文
     * @param isSpeed 是否是极速版
     * @return
     */
    public PlatfInterfManager init(Context context, boolean isSpeed) {
        this.context = context;
        this.isSpeed = isSpeed;
        initDataService(isSpeed);
        return this;
    }

    /**
     * 上下文
     *
     * @return
     */
    public Context getContext() {
        return context;
    }

    /**
     * 包名
     *
     * @return
     */
    public String getPackageName() {
        return context.getPackageName();
    }

    /**
     * 是否是极速版
     *
     * @return
     */
    public boolean isSpeed() {
        return isSpeed;
    }


    /**
     * 开启调试模式
     *
     * @param isDebug
     * @return
     */
    public PlatfInterfManager setLogEnabled(boolean isDebug) {
        LogPrintUtils.init(isDebug);
        return this;
    }

    /**
     * 绑定网络访问
     *
     * @return
     */
    public PlatfInterfManager bindAidlWeb() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        WebManager.getInstance().bind(context);
        WebManager2.getInstance().bind(context);
        return this;
    }

    /**
     * 绑定硬件基础服务
     *
     * @return
     */
    public PlatfInterfManager bindAidlCepriService() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        HardwareManager.getInstance().bind(context);
        AIDLUtils.bind(context);
        return this;
    }

    /**
     * 绑定对表操作功能
     *
     * @return
     */
    public PlatfInterfManager bindAidlActionMeter() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        //绑定工单安全单元1.0通用方法aidl服务
        CommonUtilManager.getInstance().bind(context);
        //绑定安全单元1.0服务
        SecurityUnitManager.getInstance().bind(context);
        //绑定工单安全单元2.0通用方法aidl服务
        CommonUtilManager2.getInstance().bind(context);
        //绑定安全单元2.0服务
        SecurityUnitManager2.getInstance().bind(context);
        //绑定698服务
        Protocol698Manager.getInstance().bind(context);
        //绑定645服务
        Protocol645Manager.getInstance().bind(context);
        return this;
    }

    /**
     * 绑定376服务
     *
     * @return
     */
    public PlatfInterfManager bindAidl376() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        Protocol376Manager.getInstance().bind(context);
        return this;
    }

    public PlatfInterfManager bindAidl698() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        Protocol698Manager.getInstance().bind(context);
        return this;
    }

    /**
     * 绑定外设协议aidl服务
     *
     * @return
     */
    public PlatfInterfManager bindAidlWS() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        ProtocolWSManager.getInstance().bind(context);
        return this;
    }

    /**
     * 进口表IEC协议
     *
     * @return
     */
    public PlatfInterfManager bindAidlIEC() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        ProtocolIECManager.getInstance().bind(context);
        return this;
    }

    /**
     * 绑定扫描服务
     *
     * @return
     */
    public PlatfInterfManager bindAidlScanner() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        ScannerManager.getInstance().bind(context);
        return this;
    }

    /**
     * 电子封印
     *
     * @return
     */
    public PlatfInterfManager bindAidlSeal() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        SealManager.getInstance().bind(context);
        return this;
    }

    /**
     * GIS服务
     *
     * @return
     */
    public PlatfInterfManager BindAidlGis() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        GisManager.getInstance().init(context);
        return this;
    }

    /**
     * 图版模糊匹配
     *
     * @return
     */
    public PlatfInterfManager BindAidlImageOCR() {
        if (isSpeed()) {
            BindAidlSpeed();
            return this;
        }
        ImageOCRManager.getInstance().bind(context);
        return this;
    }

    /**
     * 极速版绑定Aidl
     *
     * @return
     */
    protected PlatfInterfManager BindAidlSpeed() {
        AIDLServiceImpl.getInstance().bindService(context);
        return this;
    }

    /**
     * 解绑AIDL
     */
    public void unBindAidl() {
        if (isSpeed())
            unBindAidlServices();
        else
            unBindAidlService();
    }

    //初始化数据服务
    protected void initDataService(boolean isSpeed) {
        if (isSpeed) {
            initContentProvider("content://com.sgcc.pda.mdrh.common.sharepreference.connect/",
                    "content://com.sgcc.pda.mdrh.common.knowls.connect/",
                    "content://com.sgcc.pda.mdrh.common.message.connect/");
            //判断是否为安全管控服务 设置启用
            SafeManager.setIsEnabled(PlatConstant.ICommon.JUDGE_SAFE_TYPE.equals(getZoomType(context)));
        } else {
            initContentProvider("content://com.sgcc.pda.data.common.sharepreference.connect/",
                    "content://com.sgcc.pda.data.common.knowls.connect/",
                    "content://com.sgcc.pda.data.common.message.connect/");
        }
    }


    /**
     * 数据服务内容提供者
     *
     * @param contentProvider       基础信息
     * @param knowlsContentProvider 知识库
     * @param messageProvider       工单更新消息
     */
    public void initContentProvider(String contentProvider, String knowlsContentProvider, String messageProvider) {
        ContentProviderDaoImpl.getInstance().init(contentProvider);
        KnowlsContentProviderDaoImpl.getInstance().init(knowlsContentProvider);
        MessageProviderDaoImpl.getInstance().init(messageProvider);
    }

    /**
     * 非极速版解绑
     */
    protected void unBindAidlService() {
        try {
            WebManager.getInstance().unBind(context);
            WebManager2.getInstance().unBind(context);

            AIDLUtils.unbind(context);
            HardwareManager.getInstance().unBind(context);

            CommonUtilManager.getInstance().unBind(context);
            SecurityUnitManager.getInstance().unBind(context);
            CommonUtilManager2.getInstance().unBind(context);
            SecurityUnitManager2.getInstance().unBind(context);
            Protocol645Manager.getInstance().unBind(context);
            Protocol698Manager.getInstance().unBind(context);

            Protocol376Manager.getInstance().unBind(context);
            ProtocolWSManager.getInstance().unBind(context);

            ScannerManager.getInstance().unBind(context);

            ImageOCRManager.getInstance().unBind();

            ProtocolIECManager.getInstance().unBind(context);

            SealManager.getInstance().unBind(context);

            GisManager.getInstance().unBindRemoteService();
            AIDLSafeServiceImpl.getInstance().unbindService(context);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 极速版解绑
     */
    protected void unBindAidlServices() {
        try {
            AIDLServiceImpl.getInstance().unbindService(context);
            AIDLSafeServiceImpl.getInstance().unbindService(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取是否极速版微应用app
     *
     * @param context 上下文
     * @return true 是，false 不是
     */
    public static boolean isZoomApp(Context context) {
        try {
            return !TextUtils.isEmpty(getZoomType(context));
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取是否极速版微应用app
     *
     * @param context 上下文
     * @return true 是，false 不是
     */
    public static String getZoomType(Context context) {
        try {
            Uri uri = Uri.parse(PlatConstant.ICommon.JUDGE_SPEED);
            return context.getContentResolver().getType(uri);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 绑定安全管控服务
     *
     * @return 对象
     */
    public PlatfInterfManager bindAidlSafe() {
        AIDLSafeServiceImpl.getInstance().bindService(context);
        return this;
    }

}
